﻿using UnityEngine;
using System.Collections.Generic;

public class Layer2D
{
	List<Plane2D> plane2DList;
	int layerCount; //LayerIndex
	public string name;
	int layerMask;
	float zPosition;
	
	public Layer2D(int layer, float zPosition, int layerMask)
	{
		plane2DList = new List<Plane2D>();
		this.layerCount = layer;
		this.name = "layer" + layer;
		this.zPosition = zPosition;
		this.layerMask = layerMask;
	}
	
	public Plane2D AddPlane2D(Plane2D plane2D)
	{
		plane2DList.Add(plane2D);
		return plane2D;
	}
	
	public Plane2D GetPlane(int index)
	{	
		try
		{
			return plane2DList[index];
		}
		
		catch(System.ArgumentOutOfRangeException)
		{
			return null;
		}
	}
	
	public float ZBuffer
	{
		get{return zPosition;}
	}
	
	public void ClearLayer()
	{
		int length = plane2DList.Count;
		Plane2D[] planeArray = plane2DList.ToArray();
		for(int i = 0; i < length; i++)
		{
			planeArray[i].Destroy();
		}
		plane2DList.Clear();
	}
	
	public void RemovePlane2D(Plane2D plane2D)
	{
		plane2DList.Remove(plane2D);
		plane2D = null;
	}
	
	public int MaskLayer
	{
		get{return layerMask;}
	}
	
	public int LayerIndex
	{
		get{return layerCount;}
	}
	public int PlaneCOunt
	{
		get{return plane2DList.Count;}
	}
	public XMLData.Map.Layer2DInfo Package()
	{
		XMLData.Map.Layer2DInfo layer2D = new XMLData.Map.Layer2DInfo();
		layer2D.layerIndex = this.layerCount;
		layer2D.name = this.name;
		layer2D.layerMask = this.layerMask;
		layer2D.zPosition = this.zPosition;
		
		layer2D.plane2DList = new XMLData.Map.Plane2DInfo[plane2DList.Count];
		int count = 0;
		foreach(Plane2D plane2D in plane2DList)
		{
			XMLData.Map.Plane2DInfo xmlPlane = new XMLData.Map.Plane2DInfo();
			xmlPlane = plane2D.Package();
			layer2D.plane2DList[count] = xmlPlane;
			count++;
		}
		return layer2D;
	}
}

public class LayerManager
{
	List<Layer2D> layers;
	int layerMask;
	float initialZPosition;
	
	public LayerManager(float initialZPosition, int layerMask)
	{
		this.layerMask = layerMask;
		layers = new List<Layer2D>();
		this.initialZPosition = initialZPosition;
	}
	public void Init(float initialZPosition, int layerMask)
	{
		this.layerMask = layerMask;
		layers = new List<Layer2D>();
		this.initialZPosition = initialZPosition;
	}
	
	public void Init(float initialZPosition)
	{
		this.layerMask = 0;
		layers = new List<Layer2D>();
		this.initialZPosition = initialZPosition;
	}
	
	public void Destroy()
	{
		cleanup();
	}
	
	void cleanup()
	{
		for(int i = layers.Count -1; i >= 0; i--)
		{
			layers[i].ClearLayer();
		}
		layers.Clear();
	}
	
	public Layer2D CreateLayer()
	{
		int layerCount = layers.Count;
		Layer2D newLayer = new Layer2D(layerCount, 
								initialZPosition - 0.05f * layerCount, 
								layerMask);
		layers.Add(newLayer);
		return newLayer;
	}
	
	
	public void removeLayer(int index)
	{
		layers[index].ClearLayer();
		layers.RemoveAt(index);
	}
	
	public Layer2D getLayer(int index)
	{
		return layers[index];
	}
	
	public int LayerCount
	{
		get{return layers.Count;}
	}
	public Layer2D getLayer(string layerName)
	{
		for(int i = layers.Count -1; i >= 0; i--)
		{
			if(layers[i].name == layerName)
			{
				return layers[i];
			}
		}
		return null;
	}
	
	/*public LayerManager Load(XMLData.Map.LayerManagerInfo layerManagerInfo)
	{
		
	}*/
	/*public XMLData.Map.LayerManagerInfo Package()
	{
		XMLData.Map.LayerManagerInfo layerManager = new XMLData.Map.LayerManagerInfo();
		layerManager.initialZPosition = initialZPosition;
		layerManager.layerMask = this.layerMask;
		layerManager.layers = new XMLData.Map.Layer2DInfo[layers.Count];
		
		int count = 0;
		foreach(Layer2D layer2d in layers)
		{
			XMLData.Map.Layer2DInfo xmlLayer2D = new XMLData.Map.Layer2DInfo();
			xmlLayer2D = layer2d.Package();
			layerManager.layers[count] = xmlLayer2D;
			count++;
		}
		return layerManager;
	}*/
}

